﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AutoInsurance
{
    public partial class newPayout : System.Web.UI.Page
    {
        string strSqlConnection = "Data Source=localhost\\sqlexpress;Initial Catalog=Autoinsurance;Integrated Security=True";
        private int id_manager;
        string id_accident;

        protected void Page_Load(object sender, EventArgs e)
        {
            try 
            {
            string role = Session["role"].ToString();
            if (role == "1")
            {
                GridView1.Visible = false;
                Panel1.Visible = false;
                Button2.Visible = false;
            }
            else
            {
                id_accident = Request.QueryString["id_accident"];


                SqlConnection sqlCon = new SqlConnection(strSqlConnection);
                sqlCon.Open();

                string strQuery = "SELECT * FROM Payout where id_accident=" + id_accident;
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(strQuery, sqlCon);
                da.Fill(ds, "Payout");
                GridView1.DataSource = ds.Tables["Payout"];
                this.DataBind();

                sqlCon.Close();
            }
                }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection connection = new SqlConnection(strSqlConnection);
                connection.Open();

                SqlCommand searchManager = new SqlCommand("select * from Manager where login=" + "'" + Session["login"] + "'", connection);
                SqlDataReader id_m = searchManager.ExecuteReader();
                id_m.Read();
                id_manager = id_m.GetInt32(0);
                connection.Close();
                connection.Open();
                SqlCommand newInsuredAccident = new SqlCommand("insert into Payout (dateofpayout ,id_accident, payout, id_manager)" +
                    "VALUES ('" + Convert.ToDateTime(TextBox1.Text) + "','"  + id_accident  + "','" + TextBox2.Text + "','" + id_manager + "')", connection);
                newInsuredAccident.ExecuteNonQuery();

                connection.Close();
                string myUrl = "newPayout.aspx?id_accident=" + id_accident;
                Response.Redirect(myUrl);
            }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }
    }
}